/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.web30.semantic.sesame;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.web30.semantic.core.model.MetaData;
import org.web30.semantic.core.model.MetaRepository;
import org.web30.semantic.core.model.Web30Exception;

/**
 *
 * @author Xiangjin.Zhou
 */
public class SesameRepository implements MetaRepository {

    private String url = null;
    private DefaultHttpClient httpConn = null;
    /**
     * Http proxy
     */
    private HttpHost httpProxy = null;
    /**
     * Constructor 
     * @param uri url for REST service
     */
    public SesameRepository(String uri) {
        this.setURI(uri);
    }
    /**
     * Set proxy 
     * @param httpProxy
     * @param httpProxyPort
     */
    public void setProxy(String httpProxy, int httpProxyPort) {
        if (httpProxy != null && httpProxyPort > 0) {
            this.httpProxy = new HttpHost(httpProxy, httpProxyPort);
        }
    }
    /**
     * Add meta data to OpenRDF repository
     * @param object
     * @throws Web30Exception
     */
    public void addMetaData(MetaData object) throws Web30Exception {
        try {
            HttpPost httppost = new HttpPost(this.url);
            httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setSoTimeout(params, 6000);
            HttpConnectionParams.setConnectionTimeout(params, 6000);
            ConnManagerParams.setTimeout(params, 6000);
            httppost.setHeader("Keep-Alive", "300");
            httppost.setHeader("Proxy-Connection", "keep-alive");
            //httppost.setHeader("Connection", "keep-alive");
            httppost.setHeader("Pragma", "no-cache");
            httppost.setHeader("Cache-Control", "no-cache");
            System.out.println(object.getTextRepresentative().toString());
            ByteArrayEntity reqEntity = new ByteArrayEntity(object.getTextRepresentative().toString().getBytes());
            reqEntity.setContentType("text/plain;charset=UTF-8");
            httppost.setEntity(reqEntity);
            HttpResponse response = this.httpConn.execute(httppost);
            StatusLine status = response.getStatusLine();
            if (status.getStatusCode() != 204) {
                throw new Web30Exception(Web30Exception.ADD_META_FAILED);
            } 
        } catch (Exception ex) {
            throw new Web30Exception(ex);
        } 
        return ;
    }

    public void setURI(String uri) {
        this.url = uri;
    }

    public String getURI() {
        return this.url;
    }
    /**
     * Initialize repository
     * @throws Web30Exception
     */
    public void initRepository() throws Web30Exception {
        this.httpConn = new DefaultHttpClient();
        if (this.httpProxy != null) {
            this.httpConn.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                                                   httpProxy);
        }
    }

    public void closeRepository() {
        this.httpConn.getConnectionManager().shutdown();
    }
}
